package com.toutiao.model

import scala.collection.mutable.{HashMap => HMap}

class SimpleIndex(val K:Int) {
  
  val Index = new Array[InvertIndex[String]](K)
  for (j <- 0 until K){
    Index(j) = new InvertIndex[String]
  }
  
  def apply(k:Int) = Index(k)

  def insert(i:Long, vs:Array[String]) {
    val j = i%K
    Index(j.toInt).insert(i,vs) // 不对单字建索引
  }
  
  def remove(i:Long,vs:Array[String]) {
    val j = i%K
    Index(j.toInt).remove(i,vs)
  }  
}


class TimeIndex(val K:Int) {
  
  val mData = new HMap[String,SimpleIndex]
  
  def getIndex = {
    mData.values.flatMap(_.Index).toArray
  }
  
  def apply(t:String) = mData(t)
  
  def isIn(v:String) = {
    val iis = getIndex
  }
  
  def insert(t:String,i:Long, vs:Array[String]) {
    if (!mData.contains(t)){
      val index = new SimpleIndex(K)
      mData(t) = index
    }
    mData(t).insert(i, vs)
  }
  
  def remove(t:String,i:Int, vs:Array[String]) {
    mData(t).remove(i,vs)
  }
  
  def remove(t:String) {
    if (mData.contains(t)){
      mData.remove(t)
    }
  }

}